/**
 * 没有引入jquery.messager.js直接alert
 */
$.messager = $.messager || {
	popup : function(msg){
		alert(msg);
	}
}

function Pagination(element, options) {
	this.$page = "string" == typeof element ? $("#"+element) : $(element);
	this.options = $.extend(Pagination.defaults, options);
	this.init();
}

Pagination.DEFAULT_STYLE = 'default'; //分页组件默认风格
Pagination.DEFAULT_PAGE_NUM = 10; //最多显示10个分页按钮

/**
 * 调用分页组件的时候可以参考默认配置
 * 
 * metadata
 */
Pagination.defaults = {
	metadata : {
		currentPage : 1, 
		totalItemCount : 0,
		totalPageCount:1, 
		showItemCount:1
	},
	style : Pagination.DEFAULT_STYLE,
	url : "#",
	method : "get",
	maxpagenum : Pagination.DEFAULT_PAGE_NUM,
	onsuccess : function(data){},
	onerror : function(data) {},
	onpageclick : function(pagination){
		pagination.submitPage();
	}
}

Pagination.prototype.init = function(){
	this.$page.children().remove();
	var $pageHtml;
	switch (this.options.style) {
	case "default":
		$pageHtml = this.buildDefaultPageHtml();
		break;

	default:
		$pageHtml = this.buildDefaultPageHtml();
		break;
	}
	
	this.$page.append($pageHtml);
}

Pagination.prototype.buildDefaultPageHtml = function(){
	var This = this;
	metadata = This.options.metadata,
	$page = $('<nav style=" text-align: center; ">'+
	  '<ul class="pagination pagination-sm"> </ul>'+
	'</nav>'),
	$prePage = $('<li><a href="javascript:void(0)" style="float:;" aria-label="Previous">上一页</a></li>');
	
	if(metadata.currentPage == 1){
		$prePage.addClass("disabled");
	}else{
		$prePage.click(null, function(e){
			$page.find(".active").prev().click();
			return false;
    	});
	}
	
	$page.find("ul").append($prePage);
	
	var totalPageCount = metadata.totalPageCount || 1;
	var tmp = 0;
	for(var i = 1; i <= totalPageCount; i ++){
		if(metadata.currentPage - i > parseInt(this.options.maxpagenum/2)){
			continue;
		}
		tmp ++;
		metadata.currentPage
		var $eachPage = $('<li><a href="javascript:void(0)">'+i+'</a></li>');
		if(metadata.currentPage == i){
			$eachPage.addClass("active");
		}
		$eachPage.on("click", function(e){
			var clickPage = parseInt($(this).text());
			metadata.currentPage = clickPage;
			This.options.onpageclick(This);
		});
		$page.find("ul").append($eachPage);
		
		if(tmp >= this.options.maxpagenum){
			break ;
		}
	}
	
	var $nextPage = $('<li><a href="javascript:void(0)" style="float:;" aria-label="Next">下一页</a></li>');
	if(metadata.currentPage >= totalPageCount){
		$nextPage.addClass("disabled");
	}else{
		$nextPage.click(null, function(e){
			$page.find(".active").next().click();
			return false;
    	});
	}
	$page.find("ul").append($nextPage);
	return $page;
}


/*Pagination.prototype.submitPage = function(){
	var $form = $('<form action="'+this.options.url+'" method="post" style="display:none;"></form>');
	$form.append('<input name="currentPage" value="'+this.options.metadata.currentPage+'" />');
	$form.append('<input name="totalItemCount" value="'+this.options.metadata.totalItemCount+'" />');
	$form.append('<input name="showItemCount" value="'+this.options.metadata.showItemCount+'" />');
	$form.appendTo(document.body);
	$form.submit();
}*/
Pagination.prototype.submitPage = function(){
	var $form = $(this.options.form);
	$form.append('<input name="currentPage" style="display:none;" value="'+this.options.metadata.currentPage+'" />');
	$form.append('<input name="totalItemCount" style="display:none;" value="'+this.options.metadata.totalItemCount+'" />');
	$form.append('<input name="showItemCount" style="display:none;" value="'+this.options.metadata.showItemCount+'" />');
	$form.submit();
}

Pagination.prototype.setoptions = function(options){
	this.options = $.extend({}, this.options || Pagination.defaults, options);
}


!function($) {
    "use strict";

    $.fn.Pagination = $.fn.pagination = $.fn.page = function(option) {
    	var args = arguments;
    	return $(this).each(function(index, item) {
    		var $this = $(item), //.parent(":first"),
    		data = $this.data("Pagination"),
    		result,
    		options = "object" != typeof option ? null: option;

    		try {
	    		if (!data) {
	    			data = new Pagination(this, options),
	        		$this.data("Pagination", data),
	        		void 0;
	    		}
	    		if ("string" == typeof option) {
	    			option = option.toLowerCase();
	    			if (!data[option]) throw "Method " + option + " does not exist";
	        			result = data[option].apply(data, Array.prototype.slice.call(args, 1))
	    		} else result = data.setoptions(option)
    		} catch (e) {
    			e = "object" == typeof e ? e.message : e;
				$.messager.popup(e);
			}
    	})
    }
}(window.jQuery);